/*
 * 后台数据请求服务
 */
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { HTTP } from '@ionic-native/http/ngx';

import { Observable } from 'rxjs';
import * as querystring from 'querystring';
import { CacheService } from './cache.service';
import { NativeService } from './native.service';

interface ResponseType {
  code: number;
  data: any;
  message: string;
  success: boolean;
}

@Injectable()
export class DataService {

  defaultHeaders: HttpHeaders = new HttpHeaders({
    'Content-Type': 'application/json',
    Ver: 'android_1.0',
  });
  headers: HttpHeaders = new HttpHeaders({
    'Content-Type': 'application/json',
    Ver: 'web_1.0',
    Token: '',
  });

  constructor(
    public http: HttpClient,
    public cache: CacheService,
    public native: NativeService,
    private nativeHttp: HTTP
  ) {
    this.setHeader();
  }

  setHeader() {
    this.cache.clientToken.subscribe(val => {
      if (val) {
        let ver = 'web_1';
        const ptype = this.native.getPlatformType();
        if (ptype === 'android') {
          ver = 'android_1.0';
        }
        if (ptype === 'ios') {
          ver = 'ios_1.0';
        }
        this.headers = new HttpHeaders({
          Ver: ver,
          Token: val,
        });
      }
    });
  }

  /**
   * 获取接口配置
   */
  getConfig(): Observable<any> {
    const url = `http://192.168.3.25:8600/api/config/getInfo`;
    const ret = this.http.post(
      url,
      {
        configId: '1000',
      },
      {
        headers: this.defaultHeaders,
      }
    );
    return ret;
  }


  // uploadIMG(file): Observable<any> {
  //   const token = this.cache.clientToken.subscribe(data => {
  //       return data;
  //   });
  //   console.log(token);
  //   const url = `http://192.168.3.119:9998/api/file/UploadFile`;
  //   const ret = this.http.post(
  //     url,
  //     {
  //       file: file,
  //       type: 'userAvatar',
  //       token: token
  //     },
  //     {
  //       headers: this.defaultHeaders,
  //     }
  //   );
  //   return ret;
  // }

  /**
   * 登录
   */
  login(user): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 100000;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        account: user.username,
        password: user.password,
        clientName: 'efos',
        clientVersion: '1.0',
        clientType: 'android'

      },
      'no_token'
    );
  }

  /**
   * 获取短信验证码
   * @param phone 手机号
   */
  getSMSVerifyCode(phone) {
    const serverCode = 101;
    const apiCode = 100003;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        phone: phone,
      },
      'no_token'
    );
  }

  /**
   * 使用短信验证码登录
   * @param phone 手机号
   * @param code 验证码
   */
  loginWithSMSVerifyCode(phone, code) {
    const serverCode = 101;
    const apiCode = 100004;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        phone: phone,
        code: code
      },
      'no_token'
    );
  }

  // 获取用户信息
  getUserInfo(): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102074;
    return this.sendRequest(
      serverCode,
      apiCode,
      {},
      'with_token'
    );
  }

  // 获取项目列表（分区）
  getProjects(companyId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102220;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        companyId: companyId
      },
      'with_token'
    );
  }

  // 获取功能点
  getFunctions(systemID): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102150;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        systemID: 2
      },
      'with_token'
    );
  }

  getCompanyInfo(): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102015;
    return this.sendRequest(
      serverCode,
      apiCode,
      {},
      'with_token'
    );
  }

  // 修改密码
  UpadatePassword(password, oldPassword): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102077;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        password: password,
        oldPassword: oldPassword
      },
      'with_token'
    );
  }

  // 修改用户名
  UpadateName(userName, phone, mail): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102078;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        userName: userName,
        phone: phone,
        mail: mail
      },
      'with_token'
    );
  }

  // 修改用户头像
  UpdateImg(fileId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102076;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        fileId: fileId
      },
      'with_token'
    );
  }

  Logout(): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 100001;
    return this.sendRequest(
      serverCode,
      apiCode,
      {},
      'with_token'
    );
  }

  /**
   * 通过公司编号获取项目列表
   */
  getProjectlist(): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103000;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        companyId: 1,
      },
      'with_token'
    );
  }

  /**
   * 通过集团编号获取项目列表
   */
  getProjectlistByCompanyId(companyId, userId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102075;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        companyId: companyId ? companyId : 1,
        // userId: userId
      },
      'with_token'
    );
  }

  // 获取平台、集团、项目界面指标
  getTarget(code, apiType, id?): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = code;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        apiType: apiType,
        id: id
      },
      'with_token'
    );
  }

  // getTarget(code, apiType, id?): Observable<ResponseType> {
  //   const serverCode = 101;
  //   const apiCode = code;
  //   return this.sendRequest(
  //     serverCode,
  //     apiCode,
  //     {
  //       apiType: apiType,
  //       id: id
  //     },
  //     'with_token'
  //   );
  // }



  /**
   * 获取系统列表
   */
  getSystemList(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103047;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId,
        apiType: 1
      },
      'with_token'
    );
  }

  /**
   * 获取项目设备列表
   */
  getDevicelist(projectId, deviceSysId?): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103040;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
        deviceSysId: deviceSysId || 0,   // 0 表示全部
      },
      'with_token'
    );
  }

  /**
   * 获取用户项目设备系统
   */
  getUserSystemList(projectId, userId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103045;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId,
        // userId: userId
      },
      'with_token'
    );
  }


  // 获取房间信息
  getRooms(areaId, deviceSysId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103040;
    let reqBody: any;
    reqBody = {
      areaId: areaId,
      deviceSysId,
      pageIndex: 1,
      pageSize: 10000,
      IsLoadParam: true
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
 * 根据房间id获取设备告警列表
 */
  getRoomAlarmlist(deviceSysId, projectId, areaId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {};
    reqBody.areaId = areaId;
    reqBody.deviceSysId = deviceSysId;
    reqBody.projectId = projectId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }


  /**
 * 获取房间参数和设备类型参数
 */
  getRoomParams(areaId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103034;
    let reqBody: any;
    reqBody = {};
    reqBody.areaId = areaId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
 * 获取房间摄像头列表
 */
  getRoomVideoList(areaId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103230;
    let reqBody: any;
    reqBody = {};
    reqBody.areaId = areaId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

   /**
 * 获取房间摄像头列表
 */
getLiftRoomVideoList(areaIds): Observable<ResponseType> {
  const serverCode = 101;
  const apiCode = 103230;
  let reqBody: any;
  reqBody = {};
  reqBody.areaIds = areaIds;
  return this.sendRequest(
    serverCode,
    apiCode,
    reqBody,
    'with_token'
  );
}

  /**
 * 获取房间单个摄像头
 */
  getRoomVideoUrl(cameraId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103234;
    let reqBody: any;
    reqBody = {};
    reqBody.cameraId = cameraId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
 * 获取房间单个摄像头
 */
  getImportantDeviceTypeByArea(areaId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 109053;
    let reqBody: any;
    reqBody = {};
    reqBody.areaId = areaId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
  * 获取设备摄像头列表
  */
  getOneByCameraId(cameraId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103245;
    let reqBody: any;
    reqBody = {};
    reqBody.cameraId = cameraId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }


  /**
   * 获取单个设备实时数据
   */
  getDeviceInfo(deviceId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103044;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        deviceId: deviceId ? deviceId : 109,
        whetherLoadProjectDeviceParam: true,
        whetherLoadProjectDeviceParamRealtime: true,
        whetherLoadProjectDeviceParamAlarmRealtime: true,
      },
      'with_token'
    );
  }

  getDeviceParamesHistoryData(projectId, deviceId, deviceParameterId, displayCount, searchTime) {
    const serverCode = 101;
    const apiCode = 109012;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId,
        deviceId: deviceId,
        deviceParameterId: deviceParameterId,
        displayCount: displayCount,
        searchTime: searchTime,
      },
      'with_token'
    );
  }

  /** ----------------------------集团及项目告警统计及能耗start------------------------------ **/


  /**
     * 获取集团告警统计
     */
  getGroupAlarmCount(companyId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102140;
    let reqBody: any;
    reqBody = {
      companyId: companyId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
    * 获取集团告警类型
    */
  getGroupAlarmType(companyId, timeType, isAscending): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102141;
    let reqBody: any;
    reqBody = {
      companyId: companyId,
      timeType: timeType,
      isAscending: isAscending
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取集团告警处理数据统计
   */

  getGroupAlarmDeal(companyId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102142;
    let reqBody: any;
    reqBody = {
      companyId: companyId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取集团告警处理_及时响应率
   */

  getGroupAlarmDealRes(companyId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102143;
    let reqBody: any;
    reqBody = {
      companyId: companyId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取集团告警处理_及时恢复率
   */

  getGroupAlarmDealRec(companyId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 102144;
    let reqBody: any;
    reqBody = {
      companyId: companyId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  getCameraParamCurve(iotId, iotType, parameterIds, searchTime, sampleCount): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 105141;
    let reqBody: any;
    reqBody = {
      iotId: iotId,
      iotType: iotType,
      parameterIds: parameterIds,
      searchTime: searchTime,
      sampleCount: sampleCount
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取项目告警统计
   */
  getAlarmCount(projectId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103094;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
    * 获取项目告警类型
    */
  getAlarmType(projectId, timeType, isAscending): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103095;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      timeType: timeType,
      isAscending: isAscending
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取项目告警处理数据统计
   */

  getAlarmDeal(projectId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103096;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取项目告警处理_及时响应率
   */

  getAlarmDealRes(projectId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103097;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  /**
   * 获取项目告警处理_及时恢复率
   */

  getAlarmDealRec(projectId, timeType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103098;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      timeType: timeType,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }



  /** ----------------------------集团及项目告警统计及能耗end------------------------------ **/

  /** ----------------------------设备及房间参数start------------------------------ **/

  /**
   * 获取设备房间参数和设备类型参数
   */
  getDeviceParams(projectId, deviceSysId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103048;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      deviceSysId: deviceSysId,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  // 根据项目id获取天气信息
  getWeatherList(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 101220;
    return this.sendRequest(serverCode, apiCode,
      {
        projectId: projectId
      }, 'with_token');
  }

  // 根据项目id及系统id获取告警列表
  getSystemsAlarmList(projectId, deviceSysId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      deviceSysId: deviceSysId
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  getDeviceRealParams(deviceIdAndDeviceParam): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103049;
    let reqBody: any;
    reqBody = {
      deviceIdAndDeviceParam: deviceIdAndDeviceParam,
      // deviceSysId: deviceSysId,
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }


  // 电梯设备
  // 根据系统id获取电梯设备
  getLifts(projectId, deviceSysId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103040;
    let reqBody: any;
    reqBody = {
      projectId,
      deviceSysId: deviceSysId,
      pageIndex: 1,
      pageSize: 10000,
      IsLoadParam: true
    };
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  // 根据设备系统id获取告警列表
  getLiftAlarm(projectId, deviceSysId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {};
    reqBody.projectId = projectId;
    reqBody.deviceSysId = deviceSysId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }

  // 根据设备系统id获取告警列表
  getAlarmBydeviceId(deviceId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {};
    reqBody.deviceId = deviceId;
    return this.sendRequest(
      serverCode,
      apiCode,
      reqBody,
      'with_token'
    );
  }


  /** ----------------------------设备及房间参数end------------------------------ **/

  /** ----------------------------洗手间start------------------------------ **/

  getWashRoom(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103030;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId,
        roomTypeId: 9,
        pageSize: 10000,
        pageIndex: 1
      },
      'with_token'
    );
  }

  getWashRoomParams(areaIds): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103086;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        areaIds: areaIds,
        deviceTypeId: 9112
      },
      'with_token'
    );
  }

// 获取洗手间问题反馈统计
  getWashFeedback(areaIds): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103029;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        areaIds: areaIds,
      },
      'with_token'
    );
  }

  getWashAlarm(projectId, alarmStartTime, alarmEndTime): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103300;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId,
        pageIndex: 1,
        pageSize: 10000,
        alarmStartTime: alarmStartTime,
        alarmEndTime: alarmEndTime
      },
      'with_token'
    );
  }


  /** ----------------------------洗手间end------------------------------ **/

  /** ----------------------------消防start------------------------------ **/
  // 获取消防设备参数
  getFiresParams(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 109052;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
      },
      'with_token'
    );
  }

  // 获取消防设备参数
  getFireParams(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 109059;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
      },
      'with_token'
    );
  }

  // 获取消防设备参数
  getFireDevices(projectId, dataType): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 109054;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
        dataType: dataType
      },
      'with_token'
    );
  }

  // 获取消防实时告警
  getFireAlarms(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 109055;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
      },
      'with_token'
    );
  }

  // 获取其他实时告警
  getFireOtherAlarms(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId
      },
      'with_token'
    );
  }

  // 获取消防历史告警
  getHistoryFireAlarms(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 109056;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
      },
      'with_token'
    );
  }

  /** ----------------------------消防end------------------------------ **/

  /** ----------------------------告警start------------------------------ **/

  baseProjectAlarmCount(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103084;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
      },
      'with_token'
    );
  }

  baseProjectAlarmList(projectIds, alarmLevel?, pageIndex?, pageSize?): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {
      projectIds: projectIds ? projectIds : [1],
    };
    if (alarmLevel) { reqBody.alarmLevel = alarmLevel; }
    if (pageIndex) { reqBody.pageIndex = pageIndex; }
    if (pageSize) { reqBody.pageSize = pageSize; }
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }


  projectAlarmCount(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103170;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId ? projectId : 1,
      },
      'with_token'
    );
  }

  projectEventList(projectId, pageIndex, pageSize): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 105090;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      pageIndex: pageIndex,
      pageSize: pageSize,
      apiType: 1,
      isPage: true
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }


  projectAlarmList(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  getRealTimeAlarmList(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103085;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
    };
    return this.sendRequest(serverCode, apiCode, reqBody)
  }

  getRestoreAlarmList(projectId, searchTime, pageIndex, pageSize): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 105070;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      searchTime: searchTime,
      pageIndex: pageIndex,
      pageSize: pageSize
    };
    return this.sendRequest(serverCode, apiCode, reqBody)
  }
  // 获取告警任务
  getAlarmTask(taskId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103174;
    let reqBody: any;
    reqBody = {
      taskId: taskId,
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  // 获取任务历史处理记录
  getAlarmHistoryTask(reqBody) {
    const serverCode = 101;
    const apiCode = 103175;
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  // 处理任务
  dealTask(reqBody) {
    const serverCode = 101;
    const apiCode = 103172;
    // let reqBody: any;
    // reqBody = reqBody;
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  // 换人
  changeMan(reqBody) {
    const serverCode = 101;
    const apiCode = 103173;
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  getMyTask(projectId, userId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 103176;
    let reqBody: any;
    reqBody = {
      projectId: projectId,
      userId: userId
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  /** ----------------------------告警end------------------------------ **/

  /** ----------------------------控制start------------------------------ **/

  // 控制界面获取群控参数
  getCotrollers(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 104034;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId
      },
      'with_token'
    );
  }

  // 控制界面群控执行
  getExecute(controlGroupId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 104041;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        controlGroupId: controlGroupId
      },
      'with_token'
    );
  }

  // 控制界面获取单控参数
  getCotroller(projectId): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 104042;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        projectId: projectId
      },
      'with_token'
    );
  }

  // 控制界面单控控制提交
  changeCotroller(deviceId, deviceParameterId, value, controlCode?): Observable<ResponseType> {
    const serverCode = 101;
    const apiCode = 104040;
    return this.sendRequest(
      serverCode,
      apiCode,
      {
        controlCode: controlCode || '',
        deviceId: deviceId,
        deviceParameterId: deviceParameterId,
        value: value === true ? 1 : 0
      },
      'with_token'
    );
  }


  /** ----------------------------控制end------------------------------ **/
  /**
   * 获取设备参数组
   */
  getSettingDeviceParamGroup(deviceTypeCode) {
    const serverCode = 101;
    const apiCode = 104014;
    let reqBody: any;
    reqBody = {
      deviceTypeCode: deviceTypeCode ? deviceTypeCode : 2,
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }

  getRealtimeData() {
    const serverCode = 1013;
    const apiCode = 1301;
    return this.sendRequest(serverCode, apiCode, {}, 'with_token');
  }

  getAudioUrl(content) {
    const serverCode = 15;
    const apiCode = 200001;
    let reqBody: any;
    reqBody = {
      content: content,
    };
    return this.sendRequest(serverCode, apiCode, reqBody, 'with_token');
  }


  /*********************物联运维&&设备告警********************/
  // 获取实时列表列表
  getIotOperationList(type, params): Observable<Object> {
    const apiCode = {
      0: 105135,
      1: 105136
    };
    const serverCode = 101;
    return this.sendRequest(serverCode, apiCode[type], params, 'with_token');
  }

  getSensorHisAlramList(params): Observable<Object> {
    const apiCode = 105142;
    const serverCode = 101;
    return this.sendRequest(serverCode, apiCode, params, 'with_token')
  }

  // 创建流程
  getProcessCreate(params, type): Observable<Object> {
    const serverCode = 101;
    return this.sendRequest(serverCode, type ? 105130 : 105084, params, 'with_token');
  }
  // 到场/处理/编辑
  getProcessProc(params, isAdd, type): Observable<Object> {
    const serverCode = 101;
    const code = type ? isAdd ? 105131 : 105138 : isAdd ? 105085 : 105089;
    return this.sendRequest(serverCode, code, params, 'with_token');
  }
  // 完成
  getProcessFinish(params, type): Observable<Object> {
    const serverCode = 101;
    return this.sendRequest(serverCode, type ? 105132 : 105086, params, 'with_token');
  }
  // 取消
  getProcessCancel(params, type): Observable<Object> {
    const serverCode = 101;
    return this.sendRequest(serverCode, type ? 105133 : 105087, params, 'with_token');
  }

  getProcessGetTask(params, type): Observable<Object> {
    const serverCode = 101;
    return this.sendRequest(serverCode, type ? 105134 : 105088, params, 'with_token');
  }

  /**
   * 客户端健康状态
   */
  sendClientHealthState() {
    const serverCode = 101;
    const apiCode = 100002;
    return this.sendRequest(serverCode, apiCode, {}, 'with_token');
  }

  sendRequest(
    serverCode,
    apiCode,
    params,
    headertype = 'with_token'
  ): Observable<ResponseType> {
    const res = this.cache.getUrl(serverCode, apiCode);
    return new Observable(observer => {
      this.getHeader(headertype).subscribe(headers => {
        this.http
          .post(res.serviceAddr + res.apiAddr, params, {
            headers: headers,
          })
          .subscribe(val => {
            observer.next(<ResponseType>val);
            observer.complete();
          });
      });
    });
  }

  sendRequestNative(serverCode, apiCode, params, headertype) {
    const res = this.cache.getUrl(serverCode, apiCode);
    return new Observable(observer => {
      this.getHeader(headertype).subscribe(keyHeader => {
        this.nativeHttp
          .post(res.serviceAddr + res.apiAddr, params, {
            'Content-Type': 'application/json',
            key: '123123213232',
            Ver: 'android_1.0',
          })
          .then(val => {
            observer.next(val);
            observer.complete();
          });
      });
    });
  }

  getHeader(type): Observable<any> {
    return new Observable(observer => {
      if (type === 'with_token') {
        this.cache.clientToken.subscribe(data => {
          const keyHeader = new HttpHeaders({
            'Content-Type': 'application/json',
            token: data,
            Ver: 'android_1.0',
          });
          observer.next(keyHeader);
          observer.complete();
        });
      } else {
        observer.next(
          new HttpHeaders({
            'Content-Type': 'application/json',
            Ver: 'android_1.0',
          })
        );
        observer.complete();
      }
    });
  }

  httpGetUtil(url: string, options: Object): Observable<Object> {
    const params = new HttpParams({
      fromString: querystring.stringify(options),
    });
    return this.http.get(url, {
      headers: this.headers,
      params: params,
    });
  }
}
